layui.use(["element", "layer", "table"], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var table = layui.table;
	var sessionStorage = window.sessionStorage;
	var userId = sessionStorage.getItem("userId");
	var userName = sessionStorage.getItem("userName");
	var ip = "http://localhost"; //后台ip
	var port = ":9014"; //端口

	/**
	 * 获取本周、本季度、本月、上月的开端日期、停止日期
	 */
	var now = new Date(); //当前日期
	var nowDayOfWeek = now.getDay(); //今天本周的第几天
	var nowDay = now.getDate(); //当前日
	var nowMonth = now.getMonth(); //当前月
	var nowYear = now.getYear(); //当前年
	nowYear += (nowYear < 2000) ? 1900 : 0; //
	if(nowDayOfWeek == 0){
		nowDayOfWeek = 7;
	}
	var lastMonthDate = new Date(); //上月日期
	lastMonthDate.setDate(1);
	lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
	var lastYear = lastMonthDate.getYear();
	var lastMonth = lastMonthDate.getMonth();
	//获得某月的天数
	function getMonthDays(myMonth) {
		var monthStartDate = new Date(nowYear, myMonth, 1);
		var monthEndDate = new Date(nowYear, myMonth + 1, 1);
		var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
		return days;
	}

	//获得本周的开端日期
	function getWeekStartDate() {
		var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
		return translate(weekStartDate);
	}

	//获得本周的停止日期
	function getWeekEndDate() {
		var weekEndDate = new Date(nowYear, nowMonth, nowDay + (8 - nowDayOfWeek));
		return translate(weekEndDate);
	}

	//获得上周的开始日期
	function getLastWeekStartDate() {
		var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
		return translate(weekStartDate);
	}

	//获得上周的结束日期
	function getLastWeekEndDate() {
		var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
		return translate(weekEndDate);
	}

	//获得本月的开始日期
	function getMonthStartDate() {
		var monthStartDate = new Date(nowYear, nowMonth, 1);
		return translate(monthStartDate);
	}

	//获得本月的结束日期
	function getMonthEndDate() {
		var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
		monthEndDate = new Date(new Date(monthEndDate.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
		return translate(monthEndDate);
	}
	//获得上月开始时间
	function getLastMonthStartDate() {
		var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
		return translate(lastMonthStartDate);
	}
	//获得上月结束时间
	function getLastMonthEndDate() {
		var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
		lastMonthEndDate = new Date(new Date(lastMonthEndDate.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
		return translate(lastMonthEndDate);
	}

	function convertObj(data) {
		var _result = [];
		for(var key in data) {
			var value = data[key];
			if(value.constructor == Array) {
				value.forEach(function(_value) {
					_result.push(key + "=" + _value);
				});
			} else {
				_result.push(key + '=' + value);
			}
		}
		return _result.join('&');
	}

	function translate(date1) {
		return date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1) : (date1.getMonth() + 1)) + "-" + (date1.getDate() < 10 ? "0" + date1.getDate() : date1.getDate()) + " " + (date1.getHours() < 10 ? "0" + date1.getHours() : date1.getHours()) + ":" + (date1.getMinutes() < 10 ? "0" + date1.getMinutes() : date1.getMinutes()) + ":" + (date1.getSeconds() < 10 ? "0" + date1.getSeconds() : date1.getSeconds());
	}

	$(".layui-btn").click(function() {
		var collection = $(".layui-btn");
		$.each(collection, function() {
			$(this).addClass("layui-btn-primary");
			$(this).removeClass("layui-btn-normal");
		})
		$(this).removeClass("layui-btn-primary");
		$(this).addClass("layui-btn-normal");
		var name = $(this).attr("id");
		var param = {};
		param["operatorId"] = userId;
		var startDate, endDate;
		switch(name) {
			case "today":
				startDate = new Date(new Date().toLocaleDateString());
				endDate = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
				param["beginTime"] = translate(startDate);
				param["finishTime"] = translate(endDate);
				break;
			case "yesterday":
				startDate = new Date(new Date(new Date().setDate(new Date().getDate() - 1)).toLocaleDateString());
				endDate = new Date(new Date(new Date(new Date().setDate(new Date().getDate() - 1)).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
				param["beginTime"] = translate(startDate);
				param["finishTime"] = translate(endDate);
				break;
			case "dayBeforYesterday":
				startDate = new Date(new Date(new Date().setDate(new Date().getDate() - 2)).toLocaleDateString());
				endDate = new Date(new Date(new Date(new Date().setDate(new Date().getDate() - 2)).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
				param["beginTime"] = translate(startDate);
				param["finishTime"] = translate(endDate);
				break;
			case "thisWeek":
				startDate = getWeekStartDate();
				endDate = getWeekEndDate();
				param["beginTime"] = startDate;
				param["finishTime"] = endDate;
				break;
			case "lastWeek":
				startDate = getLastWeekStartDate();
				endDate = getLastWeekEndDate();
				param["beginTime"] = startDate;
				param["finishTime"] = endDate;
				break;
			case "thisMonth":
				startDate = getMonthStartDate();
				endDate = getMonthEndDate();
				param["beginTime"] = startDate;
				param["finishTime"] = endDate;
				break;
			case "lastMonth":
				startDate = getLastMonthStartDate();
				endDate = getLastMonthEndDate();
				param["beginTime"] = startDate;
				param["finishTime"] = endDate;
				break;
			case "all":
				break;
		}

		table.render({
			method: "get",
			url: ip + port + '/message/searchByPage',
			elem: '#task',
			where: param,
			page: true,
			skin: 'nob',
			cols: [
				[{
						field: "id",
						title: "ID",
						width: 100
					},
					{
						field: "date",
						title: "日期",
						width: 180
					},
					{
						field: "operator",
						title: "操作人",
						width: 100
					},
					{
						field: "action",
						title: "动作",
						width: 150
					},
					{
						field: "object",
						title: "对象",
						width: 80
					},
					{
						field: "objectName",
						title: "对象名称",
						width: 500,
						style: "cursor:pointer;color:blue"
					}
				]
			],
			parseData: function(res) {
				return {
					"code": 0,
					"count": res.records,
					"data": res.data
				}
			}
		});
	})
	$("#today").click();
});